package cn.hloger.spider.demo.carDemo.carDemo;

import com.alibaba.fastjson.JSONObject;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.time.LocalDateTime;

/**
 * TODO
 *
 * @author huanglong
 * @Description TODO
 * @data 2022/9/12
 */
public class CarService {

    private static String url = "http://222.85.190.117:60004/api/mercury/park/heartbeat";
    private static String testUrl = "http://jieshun.zcreate.com.cn:8888/api/mercury/park/heartbeat";


    public static void main(String[] args) {
        LocalDateTime dateTime = LocalDateTime.now();
        JSONObject json = new JSONObject();
        json.put("park_id", EncryptUtils.park_id);
        json.put("total_parking_number", 1);
        json.put("remain_parking_number", 1);
        json.put("open_parking_number", 1);
        json.put("reserved_parking_number", 1);
        json.put("event_time", DateUtils.getCarTime(dateTime));
        //加密,如果有不需要加密的参数如:image 就在这个之后添加
        JSONObject encryptJson = EncryptUtils.encrypt(json, dateTime);
        //请求
        String post = post(url, encryptJson);
        System.out.println(post);
    }


    public static String post(String url, JSONObject json) {
        //请求
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<String> requestEntity = new HttpEntity<>(json.toJSONString(), headers);

        ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, requestEntity, String.class);
        return responseEntity.getBody();
    }
}
